// 时间转换函数：将时间转为中国时区并格式化为 'YYYY-MM-DD HH:mm:ss' 格式
const formatTime = (time) => {
    const date = new Date(time); // 将ISO格式时间转为Date对象
    return date.toLocaleString('zh-CN', {
        timeZone: 'Asia/Shanghai',
        hour12: false // 24小时制
    }).replace(/\//g, '-').replace(',', ''); // 格式化为 'YYYY-MM-DD HH:mm:ss'
};

// 设备转换函数
export const convertDevices = (devices) => {
    return devices.map(item => {
        const statistic = item["statistics"][0];
        const statisticValue = statistic["statisticName"] === "空气温度" || statistic["statisticName"] === "空气相对湿度"
            ? statistic["statisticValue"] / 10
            : statistic["statisticValue"];

        return {
            "deviceName": item["deviceName"],
            "deviceNumber": item["deviceNumber"],
            "deviceType": item["deviceType"],
            "deviceUnit": item["deviceUnit"],
            "deviceAddress": item["deviceAddress"],
            "statisticValue": statisticValue,
            "statisticPhoto": statistic["statisticPhoto"],
        };
    });
};

// 统计数据转换函数
export const convertStatistics = (devices) => {
    return devices.map(device => {
        return device['statistics'].map(item => {
            const statisticValue = item["statisticName"] === "空气温度" || item["statisticName"] === "空气相对湿度"
                ? item["statisticValue"] / 10
                : item["statisticValue"];

            return {
                'statisticValue': statisticValue,
                'createTime': item['createTime'],
                'time': formatTime(item['createTime']), // 格式化时间为中国时区
                'statisticPhoto': item['statisticPhoto']
            };
        }).sort((a, b) => new Date(a['createTime']).getTime() - new Date(b['createTime']).getTime()); // 时间排序
    });
};

// 获取图表类型的函数
export const getChartType = (chartName) => {
    switch (chartName) {
        case "土壤温度":
        case "土壤pH值":
            return "bar";
        default:
            return "line";
    }
};
